<?php
/*
	本插件：$this->model('order')->方法名();
	其他地方调用：model('jy_kabao/order')->方法名();
*/
namespace jy_kabao;
defined('BY_JYA') or exit('error');
class order extends \plugin{
	function add_order($post=array()){
		$codeid=intval($post['codeid']);
		if(!$codeid){
			exi('卡片不存在！','error');
		}
		$batch_code=pdo_get('jy_kabao_batch_code',array('uniacid'=>$_SESSION['uniacid'],'id'=>$codeid));
		if($batch_code['status']>1){
			exi('卡片奖励已被领取！请勿重复操作！','error');
		}
		$item=pdo_get('jy_kabao_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$batch_code['listid']));
		$item['set']=json_decode($item['set'],true);
		$credit_types=credit_types();
		$status=5;
		foreach($item['set'] as &$sv){
			if(!$sv['type']){
				$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['goodsid']),array('title','image','price','subtitle','type'));
				$status=1;
			}elseif($sv['type']==1){
				$coupon=model('jy_weishop/coupon')->intcoupon($sv['couponid']);
				$sv['coupon']=array_fields(array('title2','title3','title'),$coupon);
				$sendnum=$sv['num']?:1;
				for($i=0;$i<$sendnum;$i++){
					model('jy_weishop/coupon')->getcoupon($sv['couponid'],$_SESSION['uid'],1);
				}
				$sv['status']=5;
			}elseif($sv['type']==2){
				$red=array(
					'mid'=>$_SESSION['uid'],
					'money'=>$sv['num'],
					'plugin'=>'jy_kabao',
					'remark'=>'卡包兑奖现金红包',
				);
				cfc('red_pack')->get_money($red);
				$sv['status']=5;
			}elseif($sv['type']==3){
				$sv['credit_title']=$credit_types[$sv['credit_type']];
				if($sv['credit_type'] && $sv['num']){
					cfc('core')->member_credit($_SESSION['uid'],'credit'.$sv['credit_type'],$sv['num'],1,'jy_kabao','卡包兑奖');
				}
				$sv['status']=5;
			}
		}
		$ordersn='SN'.get_ordersn('jy_kabao_order','ordersn');
		$data=array(
			'codeid'=>$codeid,
			'batchid'=>$batch_code['batchid'],
			'listid'=>$item['id'],
			'workerid'=>$batch_code['workerid'],
			'storeid'=>$batch_code['storeid'],
			'pluginname'=>$batch_code['pluginname'],
			'type'=>$batch_code['type'],
			'ordersn'=>$ordersn,
			'jiangs'=>ijson_encode($item['set']),
			'total'=>1,
			'status'=>$status,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kabao_order',$data);
		$orderid=pdo_insertid();
		$batch=pdo_get('jy_kabao_batch',array('uniacid'=>$_SESSION['uniacid'],'id'=>intval($batch_code['batchid'])),array('id','qufee','qumid'));
		pdo_update('jy_kabao_batch_code',array('qumid'=>$batch['qumid'],'qufee'=>$batch['qufee'],'status'=>2,'orderid'=>$orderid,'gettime'=>time()),array('uniacid'=>$_SESSION['uniacid'],'id'=>$codeid));
	}
	function add_payorder($goods=array()){
		$goods['set']=json_decode($goods['set'],true);
		$credit_types=credit_types();
		foreach($goods['set'] as &$sv){
			if(!$sv['type']){
				$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['goodsid']),array('title','image','price','subtitle','type'));
			}elseif($sv['type']==1){
				$coupon=model('jy_weishop/coupon')->intcoupon($sv['couponid']);
				$sv['coupon']=array_fields(array('title2','title3','title'),$coupon);
			}elseif($sv['type']==2){
			}elseif($sv['type']==3){
				$sv['credit_title']=$credit_types[$sv['credit_type']];
			}
		}
		$ordersn='SN'.get_ordersn('jy_kabao_order','ordersn');
		$data=array(
			'listid'=>$goods['id'],
			'ordersn'=>$ordersn,
			'jiangs'=>ijson_encode($goods['set']),
			'total'=>1,
			'lastnum'=>1,
			'payfee'=>$goods['buyprice'],
			'status'=>6,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kabao_order',$data);
		$orderid=pdo_insertid();
		return $orderid;
	}
	function add_diyorder($postdata=array(),$oneprice=0,$totalprice=0){
		$jiangs=$postdata['jiangs'];
		$credit_types=credit_types();
		$price=0;
		foreach($jiangs as &$sv){
			if(!$sv['type']){
				$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['goodsid']),array('title','image','price','subtitle','type'));
				$price +=$sv['goods']['price'];
			}elseif($sv['type']==1){
				$coupon=model('jy_weishop/coupon')->intcoupon($sv['couponid']);
				$sv['coupon']=array_fields(array('title2','title3','title'),$coupon);
				$price +=intval($coupon['deduct']);
			}elseif($sv['type']==2){
				$price +=$sv['num'];
			}elseif($sv['type']==3){
				$sv['credit_title']=$credit_types[$sv['credit_type']];
				$price +=$sv['num'];
			}
			
		}
		$ordersn='SN'.get_ordersn('jy_kabao_order','ordersn');
		$diyinfo=array(
			'title'=>$postdata['title'],
			'desc'=>$postdata['desc'],
			'image'=>$postdata['image'],
			'price'=>$oneprice,
		);
		$data=array(
			'diyinfo'=>ijson_encode($diyinfo),
			'ordersn'=>$ordersn,
			'jiangs'=>ijson_encode($jiangs),
			'total'=>$postdata['total'],
			'lastnum'=>$postdata['total'],
			'payfee'=>$totalprice,
			'status'=>6,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kabao_order',$data);
		$orderid=pdo_insertid();
		return $orderid;
	}
	function add_songorder($orderid=0,$ismy=false,$songlog=array()){
		$order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['status'] || $order['lastnum']<=0){
			exi('该礼包已送完或礼包已兑换，不能被赠送领取！','error');
		}
		$ordersn='SN'.get_ordersn('jy_kabao_order','ordersn');
		$data=array(
			'listid'=>$order['listid'],
			'diyinfo'=>$order['diyinfo'],
			'ordersn'=>$ordersn,
			'jiangs'=>$order['jiangs'],
			'total'=>1,
			'lastnum'=>1,
			'frommid'=>$order['mid'],
			'fromorderid'=>$order['id'],
			'status'=>0,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_kabao_order',$data);
		pdo_update('jy_kabao_order',array('lastnum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['lastnum']==1){
			pdo_update('jy_kabao_order',array('status'=>$ismy?1:7),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}
		pdo_update('jy_kabao_songlog',array('status'=>1,'mid'=>$_SESSION['uid'],'gettime'=>TIMESTAMP),array('id'=>$songlog['id'],'uniacid'=>$_SESSION['uniacid']));
		return $orderid;
	}
	function get_jiangs($orderid=0){
		$order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['status'] || $order['lastnum']<=0){
			exi('该礼包已送完或礼包已兑换！','error');
		}
		$jiangs=json_decode($order['jiangs'],true);
		$status=5;
		foreach($jiangs as $sv){
			if(!$sv['type']){
				$sv['goods']=pdo_get('jy_weishop_goods',array('id'=>$sv['goodsid']),array('title','image','price','subtitle','type'));
				$status=1;
			}elseif($sv['type']==1){
				model('jy_weishop/coupon')->getcoupon($sv['couponid'],$_SESSION['uid'],1);
			}elseif($sv['type']==2){
				$red=array(
					'mid'=>$_SESSION['uid'],
					'money'=>$sv['num'],
					'plugin'=>'jy_kabao',
					'remark'=>'卡包兑奖现金红包',
				);
				cfc('red_pack')->get_money($red);
				$sv['status']=5;
			}elseif($sv['type']==3){
				if($sv['credit_type'] && $sv['num']){
					cfc('core')->member_credit($_SESSION['uid'],'credit'.$sv['credit_type'],$sv['num'],1,'jy_kabao','卡包兑奖');
				}
			}
		}
		if($order['lastnum']==1){
			pdo_update('jy_kabao_order',array('status'=>$status),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}else{
			pdo_update('jy_kabao_order',array('lastnum -='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
			$ordersn='SN'.get_ordersn('jy_kabao_order','ordersn');
			$data=array(
				'listid'=>$order['listid'],
				'diyinfo'=>$order['diyinfo'],
				'ordersn'=>$ordersn,
				'jiangs'=>$order['jiangs'],
				'total'=>1,
				'lastnum'=>1,
				'frommid'=>$order['mid'],
				'fromorderid'=>$order['id'],
				'status'=>$status,
			);
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$order['mid'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('jy_kabao_order',$data);
		}
		return $orderid;
	}
	function upsonglog($orderid=0,$posterimg=0,$uptime=false,$sn=''){
		$isadd=pdo_get('jy_kabao_songlog',array('sn'=>$sn,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
		$basic=plugin('jy_shoplibao')->plugin_setting('basic');
		if($basic['songtime']){
			$endtime=TIMESTAMP+$basic['songtime']*24*3600;
		}
		if($isadd){
			if($uptime){
				pdo_update('jy_kabao_songlog',array('endtime'=>$endtime,'uptime'=>TIMESTAMP),array('status'=>0,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid));
			}
			return '';
		}
		$order=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		$data=array(
				'orderid'=>$orderid,
				'fmid'=>$order['mid'],
				'poster'=>$posterimg,
				'sn'=>$sn,
				'status'=>0,
				'endtime'=>$endtime,
				'uptime'=>TIMESTAMP,
				'createtime'=>TIMESTAMP,
		);
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_kabao_songlog',$data);
		return '';
	}
	public function refund_pay($id=0){
		$item=pdo_get('jy_kabao_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		if($item['status']){
			exi('该订单不支持退款！','error');
		}
		$item['lsprice']=$item['lastnum']<$item['total']?$item['lastnum']*$item['payfee']/$item['total']:$item['payfee'];
		$paylog=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pay_id']));
		if(!$paylog['id']){
			exi('该订单的支付记录不存在！','error');
		}
		$transaction_id=$paylog['transaction_id'];
		if($paylog['pay_type']=='credit' || $paylog['pay_type']=='apipay'){
			$pay_out_id=model('core/member')->add_credit2($paylog['mid'],$item['lsprice'],'订单退款-'.$item['id'],'jy_kabao');
		}elseif(strpos($paylog['pay_type'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($paylog['mid'],$paylog['pay_type'],$item['lsprice'],true,'','订单退款');
		}elseif($paylog['pay_type']=='wechat'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有微信支付号！','error');
			}
			$paylog['transid']=$transaction_id;
			$paylog['money']=$item['lsprice'];
			$paylog['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($paylog['mid'],$paylog,$noexi);
		}elseif($paylog['paytype']=='alipay'){
			if(!$transaction_id){
				if($noexi){
					return false;
				}
				exi('退款失败，没有支付宝支付号！','error');
			}
			$params=array(
				'money'=>$item['lsprice'],
				'ordersn'=>$item['ordersn'],
				'transid'=>$transaction_id,
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->refund_out($paylog['mid'],$params,$noexi,'alipay');
		}else{
			$params=array(
				'money'=>$item['lsprice'],
				'ordersn'=>$item['ordersn'],
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->pay_out($paylog['mid'],$params,$noexi);
		}
		$update=array(
			'status'=>9,
			'tuifee'=>$item['lsprice'],
			'tuitime'=>TIMESTAMP,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('jy_kabao_order',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
}
?>